package com.example.reader.ui.main.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.utils.LogUtils;
import com.example.reader.R;
import com.example.reader.net.resp.BookItem;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/7.
 */
public class BookshelfAdapter extends RecyclerView.Adapter<BookshelfAdapter.MyViewHolder> {

    public static final int HEADER_VIEW = 10;
    public static final int NORMAL_VIEW = 0;

    OnItemClickListener onClickListener;
    OnItemLongClickListener onLongClickListener;


    Context context;
    private ArrayList<BookItem> items;

    public BookshelfAdapter(Context context) {
        this.context = context;
    }

    public void setData(ArrayList<BookItem> items) {
        this.items = items;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEADER_VIEW) {
            return new MyViewHolder(headerView, viewType);
        } else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.fragment_bookshelf_item, parent, false);
            return new MyViewHolder(inflate, viewType);
        }
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (holder.viewType == HEADER_VIEW) {

        } else {
            BookItem bookItem = items.get(position - 1);
            holder.tv_book_name.setText(bookItem.name);
            setItemClick(holder, position - 1);
        }
    }

    /**
     * 设置条目点击事件
     *
     * @param holder
     * @param position
     */
    private void setItemClick(MyViewHolder holder, final int position) {
        holder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.i("apdapter ->onclick");
                if (onClickListener != null) {
                    onClickListener.onClick(view, position);
                }
            }
        });
        holder.ll_item.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (onClickListener != null) {
                    onLongClickListener.onLongClick(view, position);
                }
                return false;
            }
        });

    }

    @Override
    public int getItemCount() {
        return 1 + (items == null ? 0 : items.size());
    }

    @Override
    public int getItemViewType(int position) {
        if (headerView != null && position == 0) {
            return HEADER_VIEW;
        } else {
            return NORMAL_VIEW;
        }
    }


    public View headerView;

    public View getHeaderView() {
        return headerView;
    }

    public void setHeaderView(View headerView) {
        this.headerView = headerView;
    }


    public void setItemOnClickListener(OnItemClickListener onClickListener) {
        this.onClickListener = onClickListener;
        notifyDataSetChanged();
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
        notifyDataSetChanged();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        public int viewType;

        @BindView(R.id.tv_last_time)
        TextView tv_last_time;

        @BindView(R.id.tv_lab)
        TextView tv_lab;

        @BindView(R.id.tv_chapter_num)
        TextView tv_chapter_num;

        @BindView(R.id.tv_book_name)
        TextView tv_book_name;

        @BindView(R.id.iv_book)
        ImageView iv_book;

        @BindView(R.id.ll_item)
        LinearLayout ll_item;

        public MyViewHolder(View itemView, int viewType) {
            super(itemView);
            this.viewType = viewType;
            if (viewType == NORMAL_VIEW) {
                ButterKnife.bind(this, itemView);
            }
        }
    }


    public interface OnItemClickListener {
        void onClick(View view, int position);
    }

    public interface OnItemLongClickListener {
        void onLongClick(View view, int position);
    }

}
